<template>
  <div class="global_company_info main_white marginTop10">
    <div class="main_tile boxdq">
      <div class="main_tile_l">
        <em></em>基本信息
      </div>
    </div>
    <div class="mainua_form marginTop20">
      <el-form
        :inline="false"
        :model="form"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="140px"
      >
        <el-form-item label="网站LOGO：" prop="pic">
          <div class="main_upImg marginTop5">
            <el-upload
              :action="uploadPath"
              list-type="picture-card"
              :on-success="handSuccess"
              :show-file-list="false"
            >
              <span slot="default">
                <svg aria-hidden="true" class="main_page_svg" width="32" height="27">
                  <use xlink:href="#icon-commodit_camera" />
                </svg>
              </span>
            </el-upload>
            <img v-if="form.pic" :src="form.pic" class="main_avatar" />
            <div class="clearfix"></div>
          </div>
          <div class="global_cu">建议尺寸（220*80）</div>
        </el-form-item>
        <el-form-item label="网站名称：" prop="site_title">
          <el-input v-model="form.site_title" placeholder="请输入网站名称"></el-input>
        </el-form-item>
        <el-form-item label="客服电话：" prop="site_phone">
          <el-input v-model="form.site_phone" placeholder="请输入客服电话"></el-input>
        </el-form-item>
        <el-form-item label="配送中心地址：" prop="company_address">
          <el-input v-model="form.company_address" placeholder="请输入配送中心地址"></el-input>
        </el-form-item>
        <el-form-item label="收货人：" prop="receive_man">
          <el-input v-model="form.receive_man" placeholder="请输入收货人"></el-input>
        </el-form-item>
        <el-form-item label="收货电话：" prop="receive_mobile">
          <el-input v-model="form.receive_mobile" placeholder="请输入收货电话"></el-input>
        </el-form-item>
        <el-form-item label="收货地址：" prop="receive_address">
          <el-input v-model="form.receive_address" placeholder="请输入收货地址"></el-input>
        </el-form-item>
        <el-form-item prop="site_notice">
          <span slot="label">
            <el-tooltip class="item marginRight5 mainor_tei" effect="dark" placement="top">
              <div slot="content">展示的地方为移动端首页海报下方！</div>
              <i class="el-icon-question"></i>
            </el-tooltip>公告：
          </span>
          <el-input type="textarea" v-model="form.site_notice" placeholder="请输入公告内容"></el-input>
        </el-form-item>
      </el-form>
      <div class="clearfix"></div>
      <div class="newConBox_p100 paddingBottom10" id="newConBox">
        <div class="newConBox_h32">
          <div :class="['mainua_an newConBox ',{on:searchBarFixed},{marginLeft5:!searchBarFixed}]">
            <el-button type="primary" @click="preservation" v-repeatClick>保存</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { uploadPath } from "@/api/currency";
import { setting_info } from "@/api/global/globalSystem";
import { setting_company } from "@/api/currency";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  computed: {
    ...mapGetters(["avatar"]),
  },
  data() {
    return {
      // 上传图片
      uploadPath,
      // 搜索
      form: {
        site_logo: "",
        pic:"",
        site_title: "",
        site_phone: "",
        company_address: "",
        receive_man: "",
        receive_mobile: "",
        receive_address: "",
        site_notice: "",
      },
      // 监听滚动
      searchBarFixed: false,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("newConBox").offsetTop + 300
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    // 上传图片-成功
    handSuccess(res, file, fileList) {
      // console.log(res, file, fileList);
      this.form.site_logo = res.path;
      this.form.pic = res.src;
    },
    // 保存
    preservation() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          let sz = JSON.parse(JSON.stringify(this.form));
          let {pic, ...form} = sz
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
              ),
            },
            apiUrl().name,
            form
          );
          setting_company(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "保存成功",
                type: "success",
              });
            } else {
              this.$message.error(res.errmsg);
            }
          });
        }
      });
    },
    // 基本信息
    setting_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      setting_info(data).then((res) => {
        if (res.errcode == 0) {
          if (res.data.info) {
            this.form.pic = this.avatar.domain + res.data.info.site_logo;
            this.form.site_logo = res.data.info.site_logo;
            this.form.site_title = res.data.info.site_title;
            this.form.site_phone = res.data.info.site_phone;
            this.form.company_address = res.data.info.company_address;
            this.form.receive_man = res.data.info.receive_man;
            this.form.receive_mobile = res.data.info.receive_mobile;
            this.form.receive_address = res.data.info.receive_address;
            this.form.site_notice = res.data.info.site_notice;
          }
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 基本信息
    this.setting_info();
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.global_company_info {
  .mainua_form {
    .mainua_an {
      /deep/.el-form-item__content {
        margin-left: 0px !important;
      }
      .reset {
        background: $subActive;
        color: $menuText;
        border-color: $subActive;

        &:hover,
        &:focus {
          border-color: $menuActiveText;
          color: $menuActiveText;
        }
      }
    }
  }
  padding: 10px 10px 10px 10px;
  margin: 0 15px 0 15px;
  /deep/.el-input {
    width: 400px;
  }
  /deep/.el-textarea__inner {
    width: 600px !important;
    height: 200px;
  }
  .global_cu {
    font-size: 12px;
    color: #eb0a0a;
    margin-top: -10px;
    padding-left: 7px;
  }
}
</style>
